ROOT      = File.expand_path(File.dirname(__FILE__))
SRC_DIR   = File.join(ROOT, 'src')
DIST_DIR  = File.join(ROOT, 'dist')

desc "Pack source into a single file. Minify it with MINIFY=true (if minify is installed). 
Customise your distribution by using ONLY=events,tags,insertion (no spaces)."
task :build do
  if ENV["ONLY"]
    candidates = ENV["ONLY"].split(',').collect {|name| "#{SRC_DIR}/#{name}.js"}
  else
    candidates = Dir.glob(File.join(SRC_DIR, '*[^nice]*.js'))    
  end  
  # Put nice.js at the start
  candidates.insert(0, File.join(SRC_DIR, 'nice.js'))  
  # Create/open output file
  dist = File.open(File.join(DIST_DIR, 'prototype_nice.js'), 'w+')
  add_license(dist)
  # Add all the candidates
  candidates.each do |src|
    dist << File.open(src, 'r').read
    dist << "\n\n"
  end
  dist.close
end

task :package do
  
end

def add_license(file)
  open(File.join(ROOT, 'LICENSE')).each do |line|
    file << "// #{line}"
  end
  file << "\n\n"
end